package com.study.spmongodb.wwj.thread2.char07;

/**
 * @author study
 * @version 1.0
 * @date 2020/12/8 15:33
 */
public class ImmutablePerformance {
    public static void main(String[] args) {

        long startTimeStamp = System.currentTimeMillis();
        SyncObj alex1 = new SyncObj();
        alex1.setName("Alex");
        ImmutableObj alex = new ImmutableObj("Alex");
        for (long i = 0L; i < 1000000; i++) {
            System.out.println(alex1.toString());
        }
        long endTimestamp = System.currentTimeMillis();
        System.out.println(endTimestamp - startTimeStamp);

    }
}

final class ImmutableObj {
    private final String name;

    public ImmutableObj(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "ImmutableObj{" +
                "name='" + name + '\'' +
                '}';
    }
}

class SyncObj {
    private String name;

    public synchronized void setName(String name) {
        this.name = name;
    }

    @Override
    public synchronized String toString() {
        return "ImmutableObj{" +
                "name='" + name + '\'' +
                '}';
    }
}
